package com.ly.honor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.honor.entity.Game;
import com.ly.honor.entity.PlayerPlace;
import com.ly.honor.entity.dto.GameCreatDTO;
import com.ly.honor.entity.dto.GameEditDTO;
import com.ly.honor.entity.dto.GameQueryDTO;
import com.ly.honor.entity.vo.GameQueryVO;
import com.ly.honor.entity.vo.NewsQueryVO;
import com.ly.honor.exception.HonorException;
import com.ly.honor.mapper.PlayerPlaceMapper;
import com.ly.honor.service.GameService;
import com.ly.honor.mapper.GameMapper;
import com.ly.honor.util.ResultEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Base64;
import java.util.List;

import static com.ly.honor.util.ResultEnum.OBJECT_NOT_NULL;

/**
* @author 17798
* @description 针对表【bs_game】的数据库操作Service实现
* @createDate 2022-11-16 11:49:49
*/
@Service
public class GameServiceImpl extends ServiceImpl<GameMapper, Game>
    implements GameService{

    @Autowired
    private GameMapper gameMapper;

    @Autowired
    private PlayerPlaceMapper playerPlaceMapper;

    @Override
    public Page<GameQueryVO> getGamePageList(long pageNum, long pageSize, GameQueryDTO gameQueryDTO) {
        Page<GameQueryVO> gamePage = new Page<>(pageNum, pageSize);

        Page<GameQueryVO> gameQueryVOPage = gameMapper.getGamePageList(gameQueryDTO,gamePage);
        return gameQueryVOPage;
    }

    @Override
    public GameQueryVO getGameById(long id) {
        var game = gameMapper.selectById(id);

        var gameQueryVO = new GameQueryVO();
        BeanUtils.copyProperties(game,gameQueryVO);
        return gameQueryVO;
    }

    @Override
    public void insertGame(GameCreatDTO gameCreatDTO) {
        if(ObjectUtils.isEmpty(gameCreatDTO)){
            throw new HonorException(ResultEnum.OBJECT_NOT_NULL);
        }
        var game = new Game();
        BeanUtils.copyProperties(gameCreatDTO,game);
        gameMapper.insert(game);
    }

    @Override
    public void updateGame(GameEditDTO gameEditDTO) {
        var game = new Game();
        BeanUtils.copyProperties(gameEditDTO,game);
        gameMapper.updateById(game);
    }

    @Transactional(rollbackFor = HonorException.class)
    @Override
    public void removeGameById(long id) {
        playerPlaceMapper.delete(new LambdaQueryWrapper<PlayerPlace>().eq(PlayerPlace::getGameId,id));
        gameMapper.deleteById(id);
    }

    @Override
    public List<Game> getGameListDesc() {
        var wrapper = new LambdaQueryWrapper<Game>();
        wrapper.orderByDesc(Game::getHeat);
        var gameList = gameMapper.selectList(wrapper);
        return gameList;
    }
}




